#include "NFARule.h"
#include "NFARuleParser.h"
#include <map>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::ifstream;

using std::vector;
using std::string;

typedef vector<NFARule> ruleSet;
typedef ruleSet::iterator ruleIter;
typedef vector<ruleIter> ruleIterSet;

class NFARuleSet
{
public:
    void append_rule(const NFARule& t)
    {
        rules.push_back(t);
    }
    

    
    vector<ruleIter> search_by_from_state(char c)
    {
        vector<ruleIter> matched;
        for ( ruleIter it = rules.begin(); it != rules.end(); ++it )
        {
            if ( it->from == c )
                matched.push_back(it);
        }
        return matched;        
    } 

    
    
private:
    ruleSet rules;
};



class NFA
{
private:

public:
    NFARuleSet rules;
    vector<char> cloud;   
    
    NFA( std::istream& in )
    {
        NFARule finish(0, 0, 0);
        NFARule n(0, 0, 0);
        while ( (n = get_rule_from_stream(in)) != finish )
        {
            cout << n.toString() << endl;             
            rules.append_rule(n);
        }
        
        cloud.push_back('S');
    
    }    
    
    
    void feedTerminal(char t)
    {
        //cout << "feed terminal " << t <<endl;
        
        vector<char> new_cloud;

        for ( vector<char>::iterator it = cloud.begin(); it != cloud.end(); ++it )
        {
            if ( *it != final_state )
            {
                ruleIterSet matching_rules = rules.search_by_from_state(*it);
                search_by_on_terminal(matching_rules, t);
                for ( ruleIterSet::iterator it = matching_rules.begin(); it != matching_rules.end(); ++it )
                {
                    if ( (*it)->to !=  0 ) {
                        new_cloud.push_back( (*it)->to );
                    }
                    else {
                        new_cloud.push_back( final_state );
                    }
                    //cout << "FROM " << (*it)->from << " TO " << (*it)->to << " ON "<<  (*it)->on << endl;
                   
                 }
            
            }
        
        }

        cloud = new_cloud;       
        
    }

    
    bool accepted()
    {
        for ( vector<char>::iterator it = cloud.begin(); it != cloud.end(); ++it )
        {
            if ( *it == final_state )
                return 1;
        }
        return 0;
    }


    
     
     
private:
    static const int final_state = 1;  
    static const char start_state = 'S';
    
    
    ruleIterSet& search_by_on_terminal(ruleIterSet& t, char term)
    {
        for ( ruleIterSet::iterator it = t.begin(); it != t.end(); )
        {
            if ( (*it)->on != term )
                it = t.erase(it);
            else
                ++it;         
        }     
        return t;
    }
    
    
    
    
};   




int main()
{
    ifstream in("NFARules.txt");
    NFA n(in);
    
    string str;
    getline(cin, str);
    for ( int i = 0; i < str.length(); ++i )
        n.feedTerminal(str[i]);
    
    switch (n.accepted())
    {
        case 1:
            cout << "accepted" << endl;
            break;
        case 0:
            cout << "no such word" << endl;
            break;
    }    
}



   
